package org.dromara.agriculture.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.agriculture.domain.bo.AgricultureTaskLogBo;
import org.dromara.agriculture.domain.vo.AgricultureTaskLogVo;
import org.dromara.agriculture.domain.AgricultureTaskLog;
import org.dromara.agriculture.mapper.AgricultureTaskLogMapper;
import org.dromara.agriculture.service.IAgricultureTaskLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 批次任务日志Service业务层处理
 *
 * @author Robin
 * @date 2024-12-05
 */
@RequiredArgsConstructor
@Service
public class AgricultureTaskLogServiceImpl implements IAgricultureTaskLogService {

    private final AgricultureTaskLogMapper baseMapper;

    /**
     * 查询批次任务日志
     *
     * @param logId 主键
     * @return 批次任务日志
     */
    @Override
    public AgricultureTaskLogVo queryById(Long logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 分页查询批次任务日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 批次任务日志分页列表
     */
    @Override
    public TableDataInfo<AgricultureTaskLogVo> queryPageList(AgricultureTaskLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AgricultureTaskLog> lqw = buildQueryWrapper(bo);
        Page<AgricultureTaskLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的批次任务日志列表
     *
     * @param bo 查询条件
     * @return 批次任务日志列表
     */
    @Override
    public List<AgricultureTaskLogVo> queryList(AgricultureTaskLogBo bo) {
        LambdaQueryWrapper<AgricultureTaskLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AgricultureTaskLog> buildQueryWrapper(AgricultureTaskLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AgricultureTaskLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId() != null, AgricultureTaskLog::getTaskId, bo.getTaskId());
        lqw.like(StringUtils.isNotBlank(bo.getOperName()), AgricultureTaskLog::getOperName, bo.getOperName());
        lqw.eq(bo.getOperId() != null, AgricultureTaskLog::getOperId, bo.getOperId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperDes()), AgricultureTaskLog::getOperDes, bo.getOperDes());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AgricultureTaskLog::getStatus, bo.getStatus());
        lqw.eq(bo.getOrderNum() != null, AgricultureTaskLog::getOrderNum, bo.getOrderNum());
        return lqw;
    }

    /**
     * 新增批次任务日志
     *
     * @param bo 批次任务日志
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AgricultureTaskLogBo bo) {
        AgricultureTaskLog add = MapstructUtils.convert(bo, AgricultureTaskLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改批次任务日志
     *
     * @param bo 批次任务日志
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AgricultureTaskLogBo bo) {
        AgricultureTaskLog update = MapstructUtils.convert(bo, AgricultureTaskLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AgricultureTaskLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除批次任务日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
